﻿using Microsoft.AspNetCore.Mvc;
using ParkSaleSDK.Adapter;
using ParkSaleSDK.Data;
using ParkSaleSDK.Modularity;
using ParkSaleSDK.QuickTool;
using ParkSalesRootAdapter;
using ReadOnlyParkSaleAdapter;
using System;
using System.Collections.Generic;
using SystemData;
using static ParkSaleSDK.MainSystem.MainSystem;
using static System.Console;
using static SystemSetting.Manangers;
using static SystemSetting.SystemAdapters;
using BAIDUAPI.Objects;
using BAIDUAPI;
using MySqlTool;

namespace APIServer.Controllers
{
    [ApiController]
    [Route("[action]")]
    public class Controller : ControllerBase
    {
        static string GenerateSign(int location, int house, int number, string phone, string id)
        {
            return DataProcess.Encryption.RC4Encryption.RC4Encrypt_Base64($"{location}|{house}|{number}|{phone}|{id}", SignKey, System.Text.Encoding.UTF8);
        }
        static bool ReadFromSign(out User user, out OwnedHouse house, string sign)
        {
            sign = sign.Replace(' ', '+');
            user = new User();
            house = new OwnedHouse();
            try
            {
                string text = DataProcess.Encryption.RC4Encryption.RC4Decrypt_Base64(sign, SignKey, System.Text.Encoding.UTF8);

                string[] texts = text.Split('|');
                house.Location = int.Parse(texts[0]);
                house.House = int.Parse(texts[1]);
                house.Number = int.Parse(texts[2]);
                user.Phone = texts[3];
                user.ID = texts[4];
                return true;
            }
            catch (Exception ex)
            {
                WriteLine(ex.Message);
                return false;
            }


        }

        [HttpGet]
        public safe_User GetUser(string username)
        {
          var objs= r_User.Find<safe_User>(c_User.m_username, username);
            if (objs.Length > 0)
                return objs[0];
            else
                return null;
        }
        [HttpGet]
        public int Pay(int id)
        {
          Order[] orders=  r_Order.Find<Order>(c_Order.m_id, id.ToString());
           
            if (orders.Length > 0)
            {
                var p = r_Park.Find<Park>(c_Parking.m_id, orders[0].Number.ToString());
                if (p.Length > 0)
                {
                    if (p[0].Status == ParkPreOrdered || orders[0].Status == Order_NeedToPay)
                    {
                        w_Order.Update(orders[0], c_Order.k_Status, Order_Success.ToString());
                        w_Park.Update(p[0], c_Parking.k_Status, ParkSoldOut.ToString());
                        w_Rights.Append(new Rights() { Belong = orders[0].Belong, Item = p[0].ID });
                        return 1;// "购买成功";
                    }
                    else
                    {
                        return 2;// "订单错误，请检查订单和车位状态";
                    }
                }
                else
                {
                    return 3;// "无订单内容车位";
                }

            }
            return 0;//"订单不存在";
        }


        [HttpGet]
        public FullMessage[] RequestMessages(string id)
        {
            var arr = r_Message.Find<MessageData>(c_Message.m_to, id);
            List<FullMessage> msgs = new List<FullMessage>();
            foreach (var i in arr)
            {
                msgs.Add(i.ToFullMessage());
            }
            return msgs.ToArray();
        }

        [HttpGet]
        public Company[] FindCompanyByType(int type)
        {
            var r = r_Company.Find<Company>(c_Company.m_Type, type.ToString());
            return r;
        }
        [HttpGet]
        public Location[] FindLocation(int belong)
        {
            var arr= r_location.Find<Location>(c_Location.m_Belong,belong.ToString());
            return arr;
           
        }
        [HttpGet]
        public LocationInfo[] AllLocations()
        {

           var arr= r_location.Find<Location>(c_Any.m_Any);
            LocationInfo[] infos = new LocationInfo[arr.Length];
            int ix = 0;
            foreach (var i in arr)
            {
                infos[ix]=new LocationInfo() {Location=i,Company= r_Company.Find<Company>(c_Company.m_id, i.Belong.ToString()),ParkNumber=Count(c_Parking.Table, c_Parking.k_Location,i.ID)};
                ix++;
            }
            return infos;
        }

        [HttpGet]
        public Company FindCompanyByID(int id)
        {
            var r = r_Company.Find<Company>(c_Company.m_id, id.ToString());
            if (r.Length > 0)
                return r[0];
            else
                return null;
        }
        Random rand = new Random((int)DateTime.Now.Ticks);


        private int InUseOrderCount(int id)
        {
            int r = 0;
            foreach (var i in r_Order.Find<Order>(c_Order.m_Status, Order_Pending.ToString()))
            {
                if (i.Number == id)
                    r++;
            }
            return r;
        }

       private int Count(string table,string where,object value)
        {
            var m = new MySqlManager(SystemSetting.SystemAdapters.Database.Server, SystemSetting.SystemAdapters.Database.UserName, SystemSetting.SystemAdapters.Database.Password, SystemSetting.SystemAdapters.Database.Database,table, SystemSetting.SystemAdapters.Database.Port);
            m.Connection.Open();
            var cmd = m.Connection.CreateCommand();
            cmd.CommandText = SelectStringMaker.CountSelectStr(table, where,value.ToString());
             var read= cmd.ExecuteReader();
            int r=-1;

            while (read.Read())
               r=read.GetInt32(0);
            m.Dispose();
            return r;
        }

        [HttpGet]
        public Advertisement[] RequestAD(int num)
        {
            var r = r_AD.Find<Advertisement>(999);
            if (r.Length <= num)
                return r;
            else
            {
                Advertisement[] ad = new Advertisement[num];
                bool[] xz=new bool[num];
                for (int i = 0; i < num; i++)
                {
                    int t = rand.Next(0, num);
                    if (xz[t])
                    {
                        --i;
                        continue;
                    }    
                    ad[i] = r[t];
                    xz[t] = true;
                }
                return ad;
            }
        }

        [HttpGet]
        public FileContentResult ADImage(int id)
        {

            string path = ImageLocation + "\\" + id + ".jpg";
            string type = "image/jpeg";
            return new FileContentResult(System.IO.File.ReadAllBytes(path), type);
        }


        [HttpGet]
        public int CancelOrder(int order)
        {
            Order[] os= r_Order.Find<Order>(c_Order.m_id, order.ToString());
            if (os.Length > 0)
            {
                try {
                    if (os[0].Status == Order_Pending || os[0].Status == Order_NeedToPay)
                    {
                        return w_Order.Update(os[0], c_Order.k_Status, "-1") ? 1 : 0;//1 取消成功,取消失败
                    }
                    else
                        return -3;//订单无法取消

                   
                }
                catch (Exception e)
                {

                    return -2;//系统错误
                }

            }
            else
                return -1;//无效订单

        }


        [HttpGet]
        public ParkResult[] FindParks(int id)
        {
            var i= r_Park.Find<Park>(c_Parking.m_Location, id.ToString());
            List<ParkResult> results = new List<ParkResult>();
            foreach (var s in i)
            {
                results.Add(new ParkResult() { Park = s, Trait = TraitInfo.IntToTriat(s.Trait),OrderedCount=s.Status!=1?-1:InUseOrderCount(s.ID),ImgPaths=new string[] { "https://tse1-mm.cn.bing.net/th/id/R-C.f5299cd170d72e65f3d62dd811383d7f?rik=TaCclJ4w7xOVHw&riu=http%3a%2f%2fpic8.huitu.com%2fres%2f20130422%2f230854_20130422104738050200_1.jpg&ehk=LCLCZsfyqYGzkxrrYwyWICrDvgQapKvy%2bjyqSiwqWms%3d&risl=&pid=ImgRaw&r=0",
                "https://pic3.zhimg.com/v2-dacfa8a10c750fe5da8f706b22ab178b_1200x500.jpg",
                "https://tse1-mm.cn.bing.net/th/id/R-C.d6c0e21410840a7bb9db43afe445b335?rik=yX9RRvpyEGr6Qg&riu=http%3a%2f%2fwww.cassj.com%2fuploads%2fallimg%2f190524%2f190530%2f1-1Z5301P3221U.jpg&ehk=OC6jJQNy3lYlT%2bJfJwcWH497ahfGlgqwUHTE04LDGRE%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1",
                "https://tse1-mm.cn.bing.net/th/id/R-C.81d0ece6cfb0c462ec4729a2040a1326?rik=bL5M%2fRd7Vsg3pg&riu=http%3a%2f%2fpic78.huitu.com%2fres%2f20160603%2f549219_20160603215420986189_1.jpg&ehk=CpnB6cBIgKteGq5VhHBFqwiLOrtoo1iNfdy2M2qrNUc%3d&risl=&pid=ImgRaw&r=0"

                } });
            }
            return results.ToArray();
        }

        [HttpGet]
        public SurveyFeature[] SurveyFeatures()
        {
            List<SurveyFeature> ls = new List<SurveyFeature>();
            foreach (var i in Survey.AllValues())
            {
                ls.Add(new SurveyFeature() { Name = i.Name(), About = i.About() });
            }
            return ls.ToArray();
        }

        [HttpPost]
        public int ReplyMessage(int id, [FromBody] object args)
        {
            string ok = args.ToString();
            var arr = r_Message.Find<MessageData>(c_Message.m_id, id.ToString());
            if (arr.Length > 0)
            {
                if (arr[0].Handled > 0)
                    return -1;//消息已处理
                else
                {
                    var handle = Message[arr[0].Type];
                    if (handle != null)
                    {

                        try
                        {
                            int final = handle.HandleMessage(arr[0].ToFullMessage(), args);
                            if (final > 0)//消息处理结果>0时才处理成功
                                w_Message.Update(arr[0], c_Message.k_handled,final.ToString());
                            return final;//消息处理结果
                        }
                        catch (Exception ex)
                        {
                            return -2;//信息处理时出错
                        }
                    }
                    else
                        return -3;//无效消息处理器
                }
            }
            else
                return 0;//无目标消息
        }
        [HttpGet]
        public PublicMessage[] Messages(string to)
        {
            var arr = r_Message.Find<MessageData>(c_Message.m_to, to);
            List<PublicMessage> ls = new List<PublicMessage>();
            foreach (var i in arr)
            {
                ls.Add(i.ToPublicMessage());
            }
            return ls.ToArray();
        }

        [HttpPost]
        public SurveyData SurveyRequest(string code, [FromBody] object args)
        {
            ISurvey survey = Survey[code];
            if (survey != null)
            {
                SurveyData data = null;
                switch (survey.WhatINeed())
                {
                    case DataNeedOption.Park:
                        data = survey.SurveyAction(r_Park, args);
                        break;
                    case DataNeedOption.Order:
                        data = survey.SurveyAction(r_Order, args);
                        break;
                    case DataNeedOption.Location:
                        data = survey.SurveyAction(r_location, args);
                        break;
                    case DataNeedOption.Company:
                        data = survey.SurveyAction(r_Company, args);
                        break;
                    case DataNeedOption.Ad:
                        data = survey.SurveyAction(r_AD, args);
                        break;
                    case DataNeedOption.User:
                        data = survey.SurveyAction(r_User, args);
                        break;
                    case DataNeedOption.Rights:
                        data = survey.SurveyAction(r_Rights, args);
                        break;
                    case DataNeedOption.UserOwnedHouse:
                        data = survey.SurveyAction(r_OwnedHouse, args);
                        break;
                }
                if (data != null)
                    return data;

            }
            return new SurveyData() { RequsetStatus = false, Result = "Error" };
        }
        API api = new API();


        [HttpPost]
        public FaceCheckResult FaceCheck([FromBody] List<byte> face)
        {
           var i= Face(face.ToArray());
            var data = new FaceCheckResult();
            if (i != null)
            {
                bool signed= wr_User.Find<User>(c_User.m_id, i.user_id).Length > 0;
                if (signed)
                    data.SIGNED = 1;
                else
                    data.SIGNED = 0;
                data.ResultCode = 1;
                data.Token= API.CreateCode(i.user_id);
                data.FaceID=i.user_id;
                return data ;
            }
                
            else
                return new FaceCheckResult() { ResultCode = 0,FaceID="NOT FOUND THIS FACE!!" };
        }

        FaceLoginResult.User_list Face(byte[] data)
        {
            var r = api.LoginUser(data, "park");
            if (r.error_code == 0)
            {

                foreach (var i in r.result.user_list)
                {
                    if (i.score >= 88)
                    {
                        return i;
                    }
                }
            }
            return null;
        }

        [HttpGet]
        public int Register(string id,string username,string password,string phone,string token)
        {

            if (username.Length < 6 || password.Length < 8)
                return -1;
            password = DataProcess.Encryption.MD5Coder.MD5Encrypt(password, System.Text.Encoding.UTF8);
            User user=new ParkSaleSDK.Data.User();
            if (API.VerifyCode(token, id))
            {
                try
                {
                    if (wr_User.Find<User>(c_User.m_username,username).Length > 0)
                        return -2;
                    if (wr_User.Find<User>(c_User.m_id,id).Length > 0)
                        return -3;
                    user.ID = id;
                    user.Phone = phone;
                    user.Password = password;
                    user.UserName = username;
                    wr_User.Append(user);
                    return 1;
                }
                catch (Exception e)
                {
                    return -4;
                }


            }
            else
            {
                return 0;
            }

        }
        private double UseDiscountEffict(Order order,List<Park> hs,OwnedHouse[] houses, UserDiscount c)
        {
            var o = order;
            return Dicount[c.Code].FinalPrice(o, houses, hs.ToArray(), c.Arg) ;
        }

        [HttpGet]
        public OuterDiscount[] UseableDiscount(int order)
        {
            List<OuterDiscount> ls =new List<OuterDiscount>();
            var r = r_Order.Find<Order>(c_Order.m_id, order.ToString());
            if (r.Length > 0)
            {

                UserDiscount[] all = r_Discount.Find<UserDiscount>(c_Discount.m_Belong, r[0].Belong);
                Rights[] rights = r_Rights.Find<Rights>(c_Rights.m_belong, r[0].Belong);
                List<Park> hs = new List<Park>();
                foreach (var i in rights)
                {
                    var res = r_Park.Find<Park>(c_Parking.m_id, i.Item.ToString());
                    if (res.Length > 0)
                        hs.AddRange(res);
                }
                OwnedHouse[] houses = r_OwnedHouse.Find<OwnedHouse>(c_OwnedHouse.m_owner, r[0].Belong);
                foreach (var i in all)
                {
                    if (i.Order != null)
                        continue;
                    var ac = Dicount[i.Code];
                    if (ac != null)
                    {
                        if (ac.DiscountUseable(r[0], houses, hs.ToArray(), i.Arg))
                        {
                            ls.Add(new OuterDiscount() {Discount=i,OrderID=order,AfterPrice=UseDiscountEffict(r[0],hs,houses,i),Content=ac.Name() });
                        }
                    }
                }
                return ls.ToArray();

            }
            return ls.ToArray();
        }

        [HttpGet]
        public DiscountInfo[] myDiscount(string id)
        {
            List<DiscountInfo> ls = new List<DiscountInfo>();
            foreach (var i in r_Discount.Find<UserDiscount>(c_Discount.m_Belong, id))
            {

                ls.Add(new DiscountInfo(Dicount[i.Code]) { Discount = i });
            }
            return ls.ToArray();
        }

        //[HttpGet]
        //public int DiscountPick(string to, string code, string arg = null)
        //{
        //    if (Dicount[code] == null)
        //        return -1;

        //    try
        //    {
        //        DateTime time = DateTime.Now;
        //        UserDicountAdapter adapter = w_Discount;
        //        adapter.Append(new UserDiscount() { PickTime = $"{time.Year}/{time.Month}/{time.Day} {time.Hour}:{time.Minute}:{time.Second}", Arg = arg != null ? arg : null, Belong = to, Code = code });
        //        return 1;
        //    }
        //    catch (Exception ex)
        //    {
        //        WriteLine(ex.Message);
        //        return 0;
        //    }

        //}

        [HttpGet]
        public ParkingRights[] FindParkRightsByID(string id)
        {
            List<ParkingRights> res = new List<ParkingRights>();
            readOnly_ParkRightAdapter rightAdapter = r_Rights;
            readOnly_ParkTableAdapter parkTableAdapter = r_Park;
            var rights = rightAdapter.Find<Rights>(c_Rights.m_belong, id);
            foreach (var i in rights)
            {
                var obj = parkTableAdapter.Find<Park>(c_Parking.m_id, i.Item.ToString());
                if (obj.Length > 0)
                {
                    res.Add(new ParkingRights() { Right = i, Park = obj[0],Trait=TraitInfo.IntToTriat(obj[0].Trait) });
                }
            }
            return res.ToArray();
        }
        bool IsHadHouse(OwnedHouse[] house, int address)
        {
            foreach (var i in house)
            {
                if (i.Location == address)
                    return true;
            }
            return false;
        }




        [HttpPost]
        public Order UseDiscount(int order, int comfirm, [FromBody] List<int> DiscountId)
        {
            Order[] orders = r_Order.Find<Order>(c_Order.m_id, order.ToString());
            if (orders.Length > 0)
            {
                var o = orders[0];
                List<OuterDiscount> discounts = new List<OuterDiscount>(UseableDiscount(order));
                Rights[] rights = r_Rights.Find<Rights>(c_Rights.m_belong, o.Belong);
                List<Park> hs = new List<Park>();
                foreach (var i in rights)
                {
                    var res = r_Park.Find<Park>(c_Parking.m_id, i.Item.ToString());
                    if (res.Length > 0)
                        hs.AddRange(res);
                }
                OwnedHouse[] houses = r_OwnedHouse.Find<OwnedHouse>(c_OwnedHouse.m_owner, o.Belong);
                foreach (var item in DiscountId)
                {
                    int id = item;
                    int index = discounts.FindIndex(delegate (OuterDiscount d)
                    {
                        return d.Discount.ID == id;
                    });
                    if (index >= 0)
                    {
                        var now = discounts[index].Discount;
                        o.Price = Dicount[now.Code].FinalPrice(o, houses, hs.ToArray(), now.Arg);
                        if (comfirm == 1)
                        {
                            w_Discount.Update(now, c_Discount.k_Order, o.ID.ToString());
                            w_Order.Update(o, c_Order.k_Price, o.Price.ToString());
                        }
                    }
                }
                return o;
            }
            return null;
        }


        //PutOrder?buyer=330182199709230055&address=4&park=6&seller=8
        [HttpGet]
        public int PutOrder(string buyer, int address, int park, int? seller)
        {
            safe_UserTableAdapter userTableAdapter = r_User;
            readOnly_OwnedHouseAdapter house = r_OwnedHouse;
            var had = r_Order.Find<Order>(c_Order.m_Number, park.ToString());
            foreach (var i in had)
            {
                if (i.Belong.Equals(buyer) && i.Status != Order_Closed)
                    return -4;//已存在订单
            }
            var r = userTableAdapter.Find<User>(c_User.m_id, buyer);
            if (r.Length > 0)
            {
                readOnly_ParkTableAdapter adapter = r_Park;
                var p = adapter.Find<Park>(c_Parking.m_id, park.ToString());
                var houses = house.Find<OwnedHouse>(c_OwnedHouse.m_owner, r[0].ID);
                if (houses.Length > 0)
                {

                    if (IsHadHouse(houses, address)|| (TraitInfo.IntToTriat(p[0].Trait).IsOpenForSale))
                    {

                        if (p.Length > 0)
                        {
                            if (p[0].Location != address)
                            {
                                return -2;//指定小区无指定车位
                            }
                            if (p[0].Status != ParkOnSale)
                            {
                                return -3;//车位状态错误,不在在售状态下无法下单
                            }
                            int s;
                            if (seller == null)
                            {
                                var ls = r_location.Find<Location>(c_Location.m_id, address.ToString());
                                if (ls.Length > 0)
                                {
                                    s = ls[0].Belong;
                                }
                                else
                                    return -6;//小区信息错误
                            }
                            else
                            {
                                s = (int)seller;
                            }
                            Order o = new Order() { Belong = buyer, Status = Order_Pending, Seller =s, Address = address, Number = park, Price = p[0].Price, Time = Helper.TimeTOString(DateTime.Now) };
                            OrderAdapter orderad = w_Order;
                            orderad.Append(o);
                            return 1;//成功
                        }
                        else
                            return -1;//无指定ID的车位信息
                    }
                    else
                        return -7;//下单人无指定小区房产
                }

            }
            return -5;//下单人信息错误
        }



        //9Ofubmdl+Ik12l/wNWkrvCKoUwNNkxNtN1H/nG4leKR2RD++udMSqw==
        [HttpGet]
        public HouseInfo[] OwnedHouses(string user)
        {
            readOnly_OwnedHouseAdapter readOnly_OwnedHouseAdapter = r_OwnedHouse;
            List<HouseInfo> info = new List<HouseInfo>();
            foreach (var i in readOnly_OwnedHouseAdapter.Find<OwnedHouse>(c_OwnedHouse.m_owner,user))
            {
                HouseInfo house = new HouseInfo() { House = i };
                var ls = r_location.Find<Location>(c_Location.m_id, i.Location.ToString());
                if (ls.Length > 0)
                {
                    house.Location = ls[0];
                }
                info.Add(house);
            }
            return info.ToArray();
        }
        //[HttpGet]
        //public int RegisterHouseInfo(string sign, string user)
        //{
        //    UserTableAdapter adapter = wr_User;
        //    OwnedHouseAdapter houseAdapter = w_OwnedHouse;
        //    User u;
        //    OwnedHouse house;
        //    if (ReadFromSign(out u, out house, sign))
        //    {
        //        var ur = adapter.Find<User>(c_User.m_id, user);
        //        if (ur.Length > 0)
        //        {
        //            if (u.Phone.Equals(ur[0].Phone))
        //            {
        //                house.Owner = u.ID;
        //                houseAdapter.Append(house);
        //                return 1;
        //            }
        //            return -2;
        //        }
        //        return 0;
        //    }
        //    return -1;
        //}

        [HttpGet]
        public OutOrder[] FindOrder(string id)
        {
            ReadableDatabaseAdapter adapter = r_Order;
            var orders= adapter.Find<Order>(c_Order.m_Belong, id);
            OutOrder[] results = new OutOrder[orders.Length];
            for (int i = 0; i < results.Length; i++)
            {
                results[i] = new OutOrder();
                results[i].Basic = orders[i];
                results[i].CompanyInfo = r_Company.Find<Company>(c_Company.m_id, orders[i].Seller.ToString())[0];
                results[i].LocationInfo =r_location.Find<Location>(c_Location.m_id, orders[i].Address.ToString())[0];
            }

            return results;
        }
        [HttpGet]
        public LoginResult Login(string username, string password)
        {
            UserTableAdapter adapter = wr_User;
            User[] result = adapter.Find<User>(c_User.m_username, username);
            if (result.Length > 0)
            {
                if (DataProcess.Encryption.MD5Coder.MD5Encrypt(password, System.Text.Encoding.UTF8).Equals(result[0].Password))
                    return new LoginResult() { result = 1, userinfo = result[0], likeInfo = result[0].Like, houseinfo = OwnedHouses(result[0].ID) };
                else
                    return new LoginResult() { result = 0 };
            }
            else
                return new LoginResult() { result = -1 };

        }
        [HttpGet]
        public TraitInfo GetTrait(int t)
        {
            return TraitInfo.IntToTriat(t);
        }

        bool intToBool(int i)
        {
            return i == 1;
        }
        [HttpGet]
        public int TranslateTrait(int underground,int light,int road,int sale,int open,int store)
        {
            TraitInfo trait=new TraitInfo();
            trait.IsNearShop = intToBool(store);
            trait.IsFullLight = intToBool(light);
            trait.IsNearRoad = intToBool(road);
            trait.IsSpecialForSale = intToBool(sale);
            trait.IsOpenForSale = intToBool(open);
            trait.IsUnderGround = intToBool(underground);
            return (int)trait;
        }

        [HttpGet]
        public LoginResult UserDataUpdate(string id)
        {
            UserTableAdapter adapter = wr_User;
            User[] result = adapter.Find<User>(c_User.m_id, id);
            if (result.Length > 0)
            {
                return new LoginResult() { result = 1, userinfo = result[0], likeInfo = result[0].Like, houseinfo = OwnedHouses(result[0].ID) };
            }
            else
                return new LoginResult() { result = 0 };
        }

        [HttpGet]
        public int UpdateUserTrait(string id, int t)
        {
            UserTableAdapter adapter = wr_User;
            User[] result = adapter.Find<User>(c_User.m_id, id);
            if (result.Length > 0)
            {

                return adapter.Update(result[0], c_User.k_Like, t.ToString())?1:-1;
            }
            else
                return 0;

        }

        [HttpGet]
        public LoginResult FaceLogin(string id,string facestr)
        {
            var r = API.VerifyCode(facestr, id);
            if (!r)
                return new LoginResult() {result=-1 };
            UserTableAdapter adapter = wr_User;
            User[] result = adapter.Find<User>(c_User.m_id,id);
            if (result.Length > 0)
            {
                return new LoginResult() { result=1,userinfo=result[0],likeInfo= result[0].Like,houseinfo=OwnedHouses(result[0].ID)};
            }
            else
                return new LoginResult() {result=0 };

        }
    }
}
